import { ComponentWithStore } from 'mobx-miniprogram-bindings'
import { userStore } from '../../store/userStore'
import { reqLogin, getuserInfo } from '../../api/user'
ComponentWithStore({
  storeBindings:
  {
    store: userStore,
    fields: ['token', 'userInfo'],
    actions: ['setToken', 'setUserInfo']
  },
  methods: {
    /**
     * 登录
     */
    loginFn() {
      wx.login({
        success: async ({ code }) => {
          if (code) {
            let { data } = await reqLogin(code)
            wx.setStorage({ key: "token", data: data.token })
            this.setToken(data.token)
            this.userFn()
          } else {
            wx.toast({ title: '授权失败，请重新授权' })

          }
        }
      })
    },
    async userFn() {
      const { data } = await getuserInfo()
      wx.setStorage({ key: 'userInfo', data })
      this.setUserInfo(data)
      wx.switchTab({
        url: '/pages/my/my',
      })
    }
  }

})
